<!--
 * @Author: HeAo
 * @Date: 2021-10-09 11:31:07
 * @LastEditTime: 2021-10-09 11:48:09
 * @LastEditors: HeAo
 * @Description: 
 * @FilePath: \web2103-2105\web2105\3-2-Vue\day02\6-双向数据绑定.html
 * 别乱动！
-->
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    #output {
      width: 100px;
      height: 50px;
      line-height: 50px;
      text-align: center;
      font-size: 20px;
      color: springgreen;
      border: 1px solid #333;
      margin: 20px 0;
    }
  </style>
</head>

<body>
  <div id="app">
    用户名：<input type="text">
    <div id="output"></div>
    <button>修改数据</button>
  </div>
  <script>
    // 1.获取dom节点
    const input = document.querySelector('input')
    const output = document.querySelector('#output')
    const btn = document.querySelector('button')

    // 2.模拟数据模型
    let data = {

    }

    // 3.监听input输入框值的改变
    input.addEventListener('input', function () {
      data.username = this.value
      // output.innerText = this.value
    }, false)

    // 4.点击btn按钮修改数据模型中的数据
    btn.addEventListener('click', function () {
      data.username = 'tom'
    }, false)

    // 5.使用Object.defineProperty完成数据劫持
    Object.defineProperty(data, 'username', {
      set(value) {
        setValue(value)
      }
    })

    // 6.封装页面渲染函数
    function setValue(value) {
      input.value = value
      output.innerText = value
    }
  </script>
</body>

</html>